package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.Cart;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for Cart
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.Cart
 */
public class CartDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(CartDAO.class);
    // property constants
    public static final String GOODS_ID = "goodsId";
    public static final String GOODS_NUMBER = "goodsNumber";
    public static final String BUYER_ID = "buyerId";

    public Cart save(Cart transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<Cart> saveList(List<Cart> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (Cart temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(Cart persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public Cart findById(java.lang.String id)
    {
        log.debug("getting Cart instance with id: " + id);
        try
        {
            Cart instance = (Cart) getSession().get("com.qianhui.pojo.Cart", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding Cart instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from Cart as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByGoodsId(Object goodsId)
    {
        return findByProperty(GOODS_ID, goodsId);
    }

    public List findByGoodsNumber(Object goodsNumber)
    {
        return findByProperty(GOODS_NUMBER, goodsNumber);
    }

    public List findByBuyerId(Object buyerId)
    {
        return findByProperty(BUYER_ID, buyerId);
    }

    public List findAll()
    {
        log.debug("finding all Cart instances");
        try
        {
            String queryString = "from Cart";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public Cart findByGoodsIdAndBuyerId(String goodsId, String buyerId)
    {
        try
        {
            String queryString = "from Cart c where c.goodsId=:goodsId and c.buyerId=:buyerId";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter("goodsId", goodsId);
            queryObject.setParameter("buyerId", buyerId);
            List tempList = queryObject.list();
            if (tempList != null && tempList.size() > 0)
            {
                return (Cart) tempList.get(0);
            }
            else
            {
                return null;
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getCartGoodsTotalCount(String buyerId)
    {
        try
        {
            String sql = "select sum(c.goodsNumber) from Cart c where c.buyerId=:buyerId";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).intValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getCartGoodsKindsCount(String buyerId)
    {
        try
        {
            String sql = "select count(*) from Cart c where c.buyerId=:buyerId";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int deleteByGoodsIdsListAndBuyerId(List<String> idList, String buyerId)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            String sql = "delete Cart c where c.goodsId in (:idList) and c.buyerId=:buyerId";
            Query query = getSession().createQuery(sql);
            query.setParameterList("idList", idList);
            query.setParameter("buyerId", buyerId);
            query.executeUpdate();
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

}